﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using BusinessLogic.Interfaces;
using Domain;
using Domain.Entities;

namespace BusinessLogic.Implementations
{
   public  class EFOrderRepository : IOrderRepository
    {

        private EFDbContext context;

        public EFOrderRepository(EFDbContext context)
        {
            this.context = context;
        }

       public IEnumerable<Order> GetOrders()
       {
           return context.Orders.ToList();
       }

       public Order GetOrderById(int id)
       {
           return context.Orders.FirstOrDefault(i => i.Id == id);
       }

       public void DeleteOrderById(int id)
       {
           context.Orders.Remove(GetOrderById(id));
           context.SaveChanges();
       }

       public void CreateOrder(Order order)
       {
           context.Orders.Add(order);
           context.SaveChanges();
       }

       public void SaveOrder(Order order)
       {
           if (order.Id == 0)
           {
               CreateOrder(order);
               return;
           }
           else
           {
               context.Entry(order).State = EntityState.Modified;
           }
           context.SaveChanges();
       }
    }
}
